/**
 *  存放 用户 数据
 * **/
import FleetIdControllerApi from '@/api/FleetIdController';
import AuthorityGroupManage from '@/api/dataManages/AuthorityGroupManage';
import * as types from '../mutation-type';
import AuthorityGroupManageDatas from './AuthorityGroupManageDatas';
import HomeApi from '../../api/HomeApi';  // 引入定义的方法
// initial state
const state = {
  GloableToolTopbody: 'dd',
  stateAllSubType: [],
  stateAllSubTypeArr: [],
  stateBaseUrl: '',
  stateBaseUrl1604: '',
  stateBaseUrl1605: '',
  stateBaseUrlHostname: '',//ip
  //在线播放http-1604、https-1614
  play_port: ':1604/',
  //下载http-1605、https-1615
  download_port: ':1605/',
  //http、https
  http: 'http://',
  stateAllSubTypeObj: {
    pocID: 1,  //POC终端  1 1
    GotaID: 1,  //Gota终端 1 2
    QchatID: 1,  //Qchat终端 1 3
    DMRID: 1,  //DMR终端 1 4

    SIPZD: 1,  //SIP终端 5 1
    RTMPSPY: 1,  //RTMP视频源 5 2
    BKQSDK: 1,  //布控球(SDK) 5
    BKQGBT28181: 1,  //布控球(GBT28181) 5
    WRJGBT28181: 1,  //无人机(GBT28181) 5
    HWSXT: 1,  //红外摄像头 5

    MNLWG: 1,  //模拟类网关 10 1
    DMRCT: 1,  //DMR 车台 10

    SIPZD201: 1,  //SIP终端 20 1

    DNRZJT: 1,  //DMR中继台21 3
    SPWG: 1,  //视频网关 21 1
    DMRWG: 1,  //DMR网关 21 2
    PSIPWG: 1,  //PSIP网关 21 2
    PDTWG: 1,  //PDT网关 21 2
    PDTZG: 1,  //PDT终端 1 4

  },
  stateAllSubTypeArr2: [1, 2, 3, 4, 5, 6, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 4],
  userInfo: {
    name: '调度员：小峰',
    userNameForLog: '',
    userName: '',
    extNumber: '',
    scsFleetId: '',
    managePriv: '',
    userLicense: [
      {
        id: 91,
        index: 0,
        name: '调度',
        switch: false,
      }, {
        id: 92,
        index: 1,
        name: '对讲',
        switch: false,
      }, {
        id: 93,
        index: 2,
        name: '消息', switch: false,
      }, {
        id: 94,
        index: 3,
        name: '坐席', switch: false,
      }, {
        id: 95,
        index: 4,
        name: '设置', switch: true,
      }, {
        id: 96,
        index: 5,
        name: '功能页', switch: false,
      }, {
        id: 97,
        index: 6,
        name: '个人主页', switch: false,
      }, {
        id: 98,
        index: 7,
        name: '系统通讯录', switch: false,
      }, {
        id: 99,
        index: 8,
        name: '历史通话', switch: false,
      }, {
        id: 100,
        index: 9,
        name: '通知查看', switch: false,
      }, {
        id: 101,
        index: 10,
        name: '会议', switch: false,
      }, {
        id: 102,
        index: 11,
        name: '广播通知', switch: false,
      }, {
        id: 103,
        index: 12,
        name: '组织架构管理', switch: false,
      }, {
        id: 104,
        index: 13,
        name: '调度台用户管理', switch: true,
      }, {
        id: 105,
        index: 14,
        name: '终端用户管理', switch: false,
      }, {
        id: 106,
        index: 15,
        name: '对讲组管理', switch: false,
      }, {
        id: 107,
        index: 16,
        name: '个人通讯录管理', switch: false,
      }, {
        id: 108,
        index: 17,
        name: '系统通讯录管理', switch: false,
      }, {
        id: 109,
        index: 18,
        name: '权限组管理', switch: true,
      }, {
        id: 110,
        index: 19,
        name: '角色管理', switch: true,
      }, {
        id: 111,
        index: 20,
        name: '紧急号码管理', switch: false,
      }, {
        id: 112,
        index: 21,
        name: '虚拟组管理', switch: false,
      },
      //新增权限：日志审计管理  坐席》强插、强拆、监听、强断、桥接
      {
        id: 113,
        index: 22,
        name: '日志审计管理', switch: false,
      },

      {
        id: 114,
        index: 23,
        name: '强插', switch: false,
      },
      {
        id: 115,
        index: 24,
        name: '强拆', switch: false,
      },
      {
        id: 116,
        index: 25,
        name: '监听', switch: false,
      },
      {
        id: 117,
        index: 26,
        name: '强断', switch: false,
      }, {
        id: 118,
        index: 27,
        name: '桥接', switch: false,
      }, {
        id: 119,
        index: 28,
        name: '代接', switch: false,
      },
    ],
    account: '',
    userId: '',
    license: [],
    roles: [71, 54],
    toolTopbody: '',
    userRoles: [],
  },
  dasTanceLicenseArry: [
    {
      id: 91,
      index: 0,
      name: '调度',
      switch: false,
    }, {
      id: 92,
      index: 1,
      name: '对讲',
      switch: false,
    }, {
      id: 93,
      index: 2,
      name: '消息', switch: false,
    }, {
      id: 94,
      index: 3,
      name: '坐席', switch: false,
    }, {
      id: 95,
      index: 4,
      name: '设置', switch: false,
    }, {
      id: 96,
      index: 5,
      name: '功能页', switch: false,
    }, {
      id: 97,
      index: 6,
      name: '个人主页', switch: false,
    }, {
      id: 98,
      index: 7,
      name: '系统通讯录', switch: false,
    }, {
      id: 99,
      index: 8,
      name: '历史通话', switch: false,
    }, {
      id: 100,
      index: 9,
      name: '通知查看', switch: false,
    }, {
      id: 101,
      index: 10,
      name: '会议', switch: false,
    }, {
      id: 102,
      index: 11,
      name: '广播通知', switch: false,
    }, {
      id: 103,
      index: 12,
      name: '组织架构管理', switch: false,
    }, {
      id: 104,
      index: 13,
      name: '调度台用户管理', switch: false,
    }, {
      id: 105,
      index: 14,
      name: '终端用户管理', switch: false,
    }, {
      id: 106,
      index: 15,
      name: '对讲组管理', switch: false,
    }, {
      id: 107,
      index: 16,
      name: '个人通讯录管理', switch: false,
    }, {
      id: 108,
      index: 17,
      name: '系统通讯录管理', switch: false,
    }, {
      id: 109,
      index: 18,
      name: '权限组管理', switch: false,
    }, {
      id: 110,
      index: 19,
      name: '角色管理', switch: false,
    }, {
      id: 111,
      index: 20,
      name: '紧急号码管理', switch: false,
    }, {
      id: 112,
      index: 21,
      name: '虚拟组管理', switch: false,
    },
    //新增权限：日志审计管理  坐席》强插、强拆、监听、强断、桥接
    {
      id: 113,
      index: 22,
      name: '日志审计管理', switch: false,
    },

    {
      id: 114,
      index: 23,
      name: '强插', switch: false,
    },
    {
      id: 115,
      index: 24,
      name: '强拆', switch: false,
    },
    {
      id: 116,
      index: 25,
      name: '监听', switch: false,
    },
    {
      id: 117,
      index: 26,
      name: '强断', switch: false,
    }, {
      id: 118,
      index: 27,
      name: '桥接', switch: false,
    }, {
      id: 119,
      index: 28,
      name: '代接', switch: false,
    },
  ],
};

// getters
const getters = {
  userInfo(state) {
    return state.name;
  },
};

// actions
// 异步方法用actions
// actions不能直接修改全局变量，需要调用commit方法来触发mutation中的方法
const actions = {
  login(context, payload) {
    context.commit('login', payload);
  },
  ChangeGloableToolTopbody(context, payload) {
    context.commit('ChangeGloableToolTopbody', payload);
  },
  logout(context) {
    context.commit('logout');
  },
  saveGlobalUserInfo(context, payload) {
    context.commit('saveGlobalUserInfo', payload);
  },
  chageGlobalUserRoles(context, payload) {
    context.commit('chageGlobalUserRoles', payload);
  },
};
//解码
const decode = function(license, daslength) {
  if (typeof license === 'undefined') {
    license = 0;
  }
  // let license_decoded = ('000000' + license.toString(2)).split('').reverse();
  let license_decoded = license.toString(2);
  console.log(license_decoded);

  function buling(a, length) {
    return a.padStart(length, 0);
  }

  let getlicense_decoded = buling(license_decoded, daslength); //'00010010'

  getlicense_decoded.split('');
  var temp = [];
  let returnRules = (() => {
    var temp = [];
    getlicense_decoded.split('').forEach(function(value, index) {
      temp[index] = value === '1';
    });
    return temp;
  })();
  console.log(returnRules);
  return returnRules;
};
//查询调度员权限信息
const queryUserLicense = function(id, fleetId, versionType) {
  AuthorityGroupManage.queryPossessLicense(
      {
        id: id,
        fleetId: fleetId,
        versionType: versionType,
      },
  ).then(res => {
    if (res.success) {
      var reslicense = res.obj;
      console.log('权限license权限license权限license权限license权限license');
      console.log(reslicense);
      state.userInfo.license = decode(reslicense);
    }
  }).catch(err => {
    // 异常情况
    console.log(err);
  });
};
//查询调度员权限/角色
const getUserRole = function(userAc) {
  let datas = [
    /* {
       name: '调度1',
       license: 45056,
     },*/ {
      name: '调度1',
      license: 1442815,
    },
  ];

  function getSelfLise(arrA, arrB) {
    for (let i = 0; i < arrA.length; i++) {
      for (let j = 0; j < arrB.length; j++) {
        if (arrA[i]) {
          arrB[i].switch = true;
        }
      }
    }
    state.dasTanceLicenseArry = arrB;
    return arrB;
  }

  let userRoles = [];
  let userLicense = [];
  let dasTanceLicenseArry = [
    {
      id: 91,
      index: 0,
      name: '调度',
      switch: false,
    }, {
      id: 92,
      index: 0,
      name: '对讲',
      switch: false,
    }, {
      id: 93,
      index: 0,
      name: '消息', switch: false,
    }, {
      id: 94,
      index: 0,
      name: '坐席', switch: false,
    }, {
      id: 95,
      index: 0,
      name: '设置', switch: false,
    }, {
      id: 96,
      index: 0,
      name: '功能页', switch: false,
    }, {
      id: 97,
      index: 0,
      name: '个人主页', switch: false,
    }, {
      id: 98,
      index: 0,
      name: '系统通讯录', switch: false,
    }, {
      id: 99,
      index: 0,
      name: '历史通话', switch: false,
    }, {
      id: 100,
      index: 0,
      name: '通知查看', switch: false,
    }, {
      id: 101,
      index: 0,
      name: '会议', switch: false,
    }, {
      id: 102,
      index: 0,
      name: '广播通知', switch: false,
    }, {
      id: 103,
      index: 0,
      name: '组织机构管理', switch: false,
    }, {
      id: 104,
      index: 0,
      name: '调度台用户管理', switch: false,
    }, {
      id: 105,
      index: 0,
      name: '终端用户管理', switch: false,
    }, {
      id: 106,
      index: 0,
      name: '对讲组管理', switch: false,
    }, {
      id: 107,
      index: 0,
      name: '个人通讯录管理', switch: false,
    }, {
      id: 108,
      index: 0,
      name: '系统通讯录管理', switch: false,
    }, {
      id: 109,
      index: 0,
      name: '权限组管理', switch: false,
    }, {
      id: 110,
      index: 0,
      name: '角色管理', switch: false,
    }, {
      id: 111,
      index: 0,
      name: '紧急号码管理', switch: false,
    }, {
      id: 112,
      index: 0,
      name: '虚拟组管理', switch: false,
    },

  ];
  /*  for (let i=0;i<datas.length;i++){
      userRoles.push(datas[i].name)
      // userLicense = decode(datas[i].license)
      let returnData = getSelfLise(decode(datas[i].license,dasTanceLicenseArry.length),dasTanceLicenseArry)
      console.log('returnData');
      console.log(returnData);
      state.userInfo.userLicense = returnData;
    }*/
  // state.userInfo.userLicense = decode(45056);

  let fh = ',';
  /*  let strTag = userRoles.map(function(obj,index){
      if(obj!=null){
        if(obj.tagName!=null){
          return obj.tagName;
        }else{
          fh = ''
        }
      }else{
        fh = ''
      }
    }).join(fh);*/
  state.userInfo.userRoles = userRoles.join(',');
  HomeApi.getUserRole(
      {
        userId: userAc,
        // userId: '1100',
      },
  ).then(res => {
    if (res.success) {
      let datas = res.obj.list;
      let userRoles = [];
      for (let i = 0; i < datas.length; i++) {
        userRoles.push(datas[i].name);
        let returnData = getSelfLise(
            decode(datas[i].license, state.dasTanceLicenseArry.length),
            state.dasTanceLicenseArry);
        state.userInfo.userLicense = returnData;
      }
      state.userInfo.userRoles = userRoles.join(',');
    }
  }).catch(err => {
    // 异常情况
    console.log(err);
  });
};
//获取终端类型id
const queryAllSubType = function() {
  HomeApi.queryAllSubType({}).then(res => {
    if (res.success) {
      let checkDatas = res.obj.list;
      let len = checkDatas.length;
      for (let i = 0; i < len; i++) {
        if (checkDatas[i].Type_ID == '1' && checkDatas[i].Sub_type_id == '1') {
          checkDatas[i]['gloUserType'] = checkDatas[i].ID;
          state.stateAllSubTypeObj.pocID = checkDatas[i].ID;
          checkDatas[i]['url'] = require(
              '@/assets/images/component/intercomGroup/userImg/user_1.png');
        } else if (checkDatas[i].Type_ID == '1' && checkDatas[i].Sub_type_id ==
            '2') {
          checkDatas[i]['gloUserType'] = checkDatas[i].ID;
          state.stateAllSubTypeObj.GotaID = checkDatas[i].ID;
          checkDatas[i]['url'] = require(
              '@/assets/images/component/intercomGroup/userImg/user_2.png');
        } else if (checkDatas[i].Type_ID == '1' && checkDatas[i].Sub_type_id ==
            '3') {
          checkDatas[i]['gloUserType'] = checkDatas[i].ID;
          state.stateAllSubTypeObj.QchatID = checkDatas[i].ID;
          checkDatas[i]['url'] = require(
              '@/assets/images/component/intercomGroup/userImg/user_3.png');
        } else if (checkDatas[i].Type_ID == '1' && checkDatas[i].Sub_type_id ==
            '4') {
          checkDatas[i]['gloUserType'] = checkDatas[i].ID;
          state.stateAllSubTypeObj.DMRID = checkDatas[i].ID;
          checkDatas[i]['url'] = require(
              '@/assets/images/component/intercomGroup/userImg/user_4.png');
        } else if (checkDatas[i].Type_ID == '5' && checkDatas[i].Sub_type_id ==
            '1') {
          checkDatas[i]['gloUserType'] = checkDatas[i].ID;
          state.stateAllSubTypeObj.SIPZD = checkDatas[i].ID;
          checkDatas[i]['url'] = require(
              '@/assets/images/component/intercomGroup/userImg/user_5.png');
        } else if (checkDatas[i].Type_ID == '5' && checkDatas[i].Sub_type_id ==
            '2') {
          checkDatas[i]['gloUserType'] = checkDatas[i].ID;
          state.stateAllSubTypeObj.RTMPSPY = checkDatas[i].ID;
          checkDatas[i]['url'] = require(
              '@/assets/images/component/intercomGroup/userImg/user_5.png');
        } else if (checkDatas[i].Type_ID == '10' && checkDatas[i].Sub_type_id ==
            '1') {
          checkDatas[i]['gloUserType'] = checkDatas[i].ID;
          state.stateAllSubTypeObj.MNLWG = checkDatas[i].ID;
          checkDatas[i]['url'] = require(
              '@/assets/images/component/intercomGroup/userImg/user_6.png');
        } else if (checkDatas[i].Type_ID == '20' && checkDatas[i].Sub_type_id ==
            '1') {
          checkDatas[i]['gloUserType'] = checkDatas[i].ID;
          state.stateAllSubTypeObj.SIPZD201 = checkDatas[i].ID;
          checkDatas[i]['url'] = require(
              '@/assets/images/component/intercomGroup/userImg/user_6.png');
        } else if (checkDatas[i].Type_ID == '21' && checkDatas[i].Sub_type_id ==
            '1') {
          checkDatas[i]['gloUserType'] = checkDatas[i].ID;
          state.stateAllSubTypeObj.spwg211 = checkDatas[i].ID;
          checkDatas[i]['url'] = require(
              '@/assets/images/component/intercomGroup/userImg/user_7.png');
        } else if (checkDatas[i].Type_ID == '21' && checkDatas[i].Sub_type_id ==
            '2') {
          checkDatas[i]['gloUserType'] = checkDatas[i].ID;
          state.stateAllSubTypeObj.DMRWG = checkDatas[i].ID;
          checkDatas[i]['url'] = require(
              '@/assets/images/component/intercomGroup/userImg/user_8.png');
        } else {
          checkDatas[i]['gloUserType'] = checkDatas[i].ID;
          checkDatas[i]['url'] = require(
              '@/assets/images/component/intercomGroup/userImg/user_999.png');
        }
      }
      state.stateAllSubType = checkDatas;
    }
  }).catch(err => {
    // 异常情况
    console.log(err);
  });
};
const getBaseUrl = function() {
  let localHref = window.location.href;//http://192.168.0.6:8081/#/home?ip=42.101.38.179&userId=wxq1
  let host = window.location.host;//192.168.0.6:8081
  let hostname = window.location.hostname;//192.168.0.6
  // var url = localHref.split('?')[0].split('/'); 	//以'#'为分割进行截取 [0]是网站域名，[1]是网站所带参数
  // let getLocalHref = url[0] + '//' + url[2] + '/' + url[3] + '/';//当前浏览器需求地址
  state.stateBaseUrl = 'http://' + hostname + ':1603';
  state.stateBaseUrl1604 = 'http://' + hostname + ':1604';
  state.stateBaseUrl1605 = 'http://' + hostname + ':1605';
  state.stateBaseUrlHostname = hostname;
};

// mutations
const mutations = {
  [types.SET_NUM](state, all) { //设置参数
    state.all = all;
  },
  login(state, payload) {
    // state.name = payload
    // localStorage.setItem(user_key, JSON.stringify(payload));
    //保存 用户登录时间
    let timespan = Date.parse(new Date());
    // localStorage.setItem(user_time, timespan);
  },
  logout(state) {
    state.userInfo = null;
    // localStorage.setItem(user_key, null);
  },
  ChangeGloableToolTopbody(state, payload) {
    state.GloableToolTopbody = payload;
    // localStorage.setItem(user_key, null);
  },
  [types.saveGlobalUserInfo](state, payload) {
    getUserRole(payload);
    queryAllSubType();
    getBaseUrl();
    FleetIdControllerApi.getfleetId({
      userId: payload,
    }).then(res => {
      // 返回集团ID
      // this.scsFleetId = res.obj[0].fid;
      // this.user.userId = res.obj[0].user_id;
      state.userInfo.scsFleetId = res.obj[0].fid;
      state.userInfo.userId = res.obj[0].user_id;
      let license = res.obj[0].license;
      let scsFleetId = res.obj[0].fid;
      let userId = res.obj[0].user_id;
      state.userInfo.scsFleetId = res.obj[0].fid;
      state.userInfo.userId = res.obj[0].user_id;
      state.userInfo.licenseHigh = res.obj[0].licenseHigh;
      state.userInfo.licenseLow = res.obj[0].licenseLow;
      state.userInfo.managePriv = res.obj[0].managePriv;
      state.userInfo.userId = res.obj[0].user_id;
      state.userInfo.userName = res.obj[0].name;//调度员名称
      state.userInfo.userNameForLog = payload;//操作记录,登录账号
      state.userInfo.extNumber = res.obj[0].extNumber;//调度员分机号
      state.userInfo.account = payload;//调度员登录账号
      // queryUserLicense(userId,scsFleetId,1)
      // queryUserLicense(93,scsFleetId,1)
      state.userInfo.license = decode(license);

    }).catch(err => {

    });
    // localStorage.setItem(user_key, null);
  },
  chageGlobalUserRoles(state, payload) {
    console.log('执行了更新角色信息');
    getUserRole(payload);
  },

};

export default {
  namespaced: true,//开启命名空间
  state,
  getters,
  actions,
  mutations,
};
